function out=get_wrapp_window(img,window_size,point,img_size,shift)
%computes the wrapping window according to a given window_size by adding
%all sub_windows
%
%
% Author: Sebastian Schmelcher         Version: 2012-06-22

out=zeros(window_size);
if(nargin<4)
    shift=[0,0];
end
yshift=shift(1);
xshift=shift(2);

if(nargin<3)
    img_size=size(img);
    ys=img_size(1);
    xs=img_size(2);
else
    ys=size(img,1);
    xs=size(img,2);
end

yw=window_size(1);
xw=window_size(2);

origin=get_origin(img_size);
yo=origin(1)-floor(yw/2);
xo=origin(2)-floor(xw/2);




%fprintf(num2str(yshift));

if(xshift==0)
    
    xstart=xo-ceil(xo/xw)*xw;
    yo=yo+ceil(xo/xw)*yshift;
    ystart=yo-ceil(yo/yw)*yw;


    if(nargin>2)   

        %find the left upper window from point
        ycount=0;
       while(xstart+xw<point(3))
           xstart=xstart+xw;
           ycount=ycount+1;
       end
       ystart=ystart+ycount*yshift;
       while(ystart+yw<point(1))
           ystart=ystart+yw;
       end   
       while(ystart>point(1))
           ystart=ystart-yw;
       end 

       ystart=ystart-point(1)+1;
       xstart=xstart-point(3)+1;

    end

    xnow=xstart;
    ynow=ystart;
    ystart_=ystart;
    
    
    while(xnow<xs)
        while(ynow<ys)

            %window is added, boundaries are taken care of
            out(max(-ynow+2,1):min(yw,yw+ys-ynow-yw+1),max(-xnow+2,1):min(xw,xw+xs-xnow-xw+1))=out(max(-ynow+2,1):min(yw,yw+ys-ynow-yw+1),max(-xnow+2,1):min(xw,xw+xs-xnow-xw+1))+img(max(ynow,1):min(ynow+yw-1,ys),max(xnow,1):min(xnow+xw-1,xs));       
            ynow=ynow+yw;
        end
        ynow=ystart+yshift;
        while(ynow>ystart_)
            ynow=ynow-yw;
        end
        while(ynow+yw<ystart_)
            ynow=ynow+yw;
        end
        ystart=ynow;
        xnow=xnow+xw;
    end
    
    
else %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    ystart=yo-ceil(yo/yw)*yw;
    xo=xo+ceil(yo/yw)*xshift;
    xstart=xo-ceil(xo/xw)*xw;


    if(nargin>2)   

        %find the left upper window from point
        xcount=0;
       while(ystart+yw<point(1))
           ystart=ystart+yw;
           xcount=xcount+1;
       end
       xstart=xstart+xcount*xshift;
       while(xstart+xw<point(3))
           xstart=xstart+xw;
       end   
       while(xstart>point(3))
           xstart=xstart-xw;
       end 

       xstart=xstart-point(3)+1;
       ystart=ystart-point(1)+1;

    end

    xnow=xstart;
    xstart_=xstart;
    ynow=ystart;
    
    while(ynow<ys)    
        while(xnow<xs)
        

            %window is added, boundaries are taken care of
            out(max(-ynow+2,1):min(yw,yw+ys-ynow-yw+1),max(-xnow+2,1):min(xw,xw+xs-xnow-xw+1))=out(max(-ynow+2,1):min(yw,yw+ys-ynow-yw+1),max(-xnow+2,1):min(xw,xw+xs-xnow-xw+1))+img(max(ynow,1):min(ynow+yw-1,ys),max(xnow,1):min(xnow+xw-1,xs));       
            xnow=xnow+xw;
        end
        xnow=xstart+xshift;
        while(xnow>xstart_)
            xnow=xnow-xw;
        end
        while(xnow+xw<xstart_)
            xnow=xnow+xw;
        end
        xstart=xnow;
        ynow=ynow+yw;
    end
end
% while(ynow<ys)
%     while(xnow<xs)
%         %window is added, boundaries are taken care of
%         out(max(-ynow+2,1):min(yw,yw+ys-ynow-yw+1),max(-xnow+2,1):min(xw,xw+xs-xnow-xw+1))=out(max(-ynow+2,1):min(yw,yw+ys-ynow-yw+1),max(-xnow+2,1):min(xw,xw+xs-xnow-xw+1))+img(max(ynow,1):min(ynow+yw-1,ys),max(xnow,1):min(xnow+xw-1,xs));       
%         xnow=xnow+xw;
%     end
%     xnow=xstart;
%     ynow=ynow+yw;
% end
